package com.stars.home.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import com.google.gson.Gson
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.adapter.SetUpAdapter
import com.stars.home.base.MBActivity
import com.stars.home.databinding.ActivitySetUpBinding
import com.stars.home.model.ServiceList
import com.stars.home.model.bean.ChoseServerBean
import com.stars.home.vmodel.SetUpVModel

/**
 *on 2022/9/23
 */
class SetUpActivity: MBActivity<SetUpVModel, ActivitySetUpBinding>() {


    private val num = 500


    //用这个
   var adapter: SetUpAdapter? = null


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        vm.getUserInfo()
        vm.getService()
        vm.user.observe(this){
            binding.etMark.setText(it.introduce)
        }
        vm.list.observe(this){
            if(null!=it){
                adapter= SetUpAdapter()
                binding.recyclerView.adapter=adapter
                adapter?.addData(it)
                adapter?.setOnItemClickListener { adapter, view, position ->

                    var item=adapter.getItem(position) as ServiceList
                    if(item.enable=="1"){

                        (adapter.getItem(position) as ServiceList).enable="0"

                    }else{

                        (adapter.getItem(position) as ServiceList).enable="1"
                    }
                    adapter.notifyItemChanged(position)

                }

            }
        }
    }


    override fun listeners() {
        super.listeners()

        binding.etMark.addTextChangedListener(object : TextWatcher {
            private var wordNum //记录输入的字数
                    : CharSequence? = null
            private var selectionStart = 0
            private var selectionEnd = 0
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(s: CharSequence, i: Int, i1: Int, i2: Int) {
                wordNum = s //实时记录输入的字数
            }

            override fun afterTextChanged(s: Editable) {
                val number = s.length

                //TextView显示剩余字数
                binding.tvNumber.text = "$number/$num"
                selectionStart = binding.etMark.selectionStart
                selectionEnd = binding.etMark.selectionEnd
                if (wordNum!!.length > num) {
                    //删除多余输入的字（不会显示出来）
                    s.delete(selectionStart - 1, selectionEnd)
                    val tempSelection = selectionEnd
                    binding.etMark.text = s
                    binding.etMark.setSelection(tempSelection) //设置光标在最后
                }
            }
        })

        binding.llSubmit.onClick{

            var remark=binding.etMark.text.toString()
            if(remark.isNullOrEmpty()){
                toast("请填写个人介绍")
                return@onClick
            }
            var choseList=ArrayList<ChoseServerBean>()
            var list=adapter?.data  as ArrayList<ServiceList>
            for(item in list){

                choseList.add(ChoseServerBean(item.technicianId,item.platformServiceId,item.enable.toInt()))

//                if(item.enable=="1"){
//                    choseList.add(ChoseServerBean(item.technicianId,item.platformServiceId,item.enable.toInt()))
//                }else{
//                    choseList.add(ChoseServerBean(item.technicianId,item.platformServiceId,item.enable.toInt()))
//
//                }
            }
//            if(choseList.size==0){
//                toast("至少开启一个服务")
//                return@onClick
//            }

            vm.toSubmit(remark, Gson().toJson(choseList))


        }


    }

    override fun vmClass(): Class<SetUpVModel> {
        return SetUpVModel::class.java
    }
    override fun binding(): ActivitySetUpBinding {
       return ActivitySetUpBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "服务设置内容"
    }
}
